package cn.baage.campus.controller;

import cn.baage.campus.model.vo.PostingAddReqVO;
import cn.baage.campus.model.vo.PostingDetailResVO;
import cn.baage.campus.model.vo.PostingQueryReqVO;
import cn.baage.campus.model.vo.PostingResVO;
import cn.baage.campus.service.PostingService;
import cn.baage.campus.utils.PageUtils;
import cn.baage.campus.utils.Result;
import cn.baage.campus.utils.ResultPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 帖子Api
 * @Author: 八阿哥
 * @url: baage.cn
 * @Version: 1.0
 **/
@Api(tags = "帖子接口")
@RestController
@RequestMapping("/posting")
@AllArgsConstructor(onConstructor_ = @Autowired)
public class PostingController {

    private final PostingService postingService;


    @GetMapping("/listPosting")
    @ApiOperation(value = "获取帖子列表")
    public ResultPage listPosting(PostingQueryReqVO postingQueryReqVO) {
        PageUtils.startPage();
        List<PostingResVO> postingList = postingService.listPosting(postingQueryReqVO);
        return ResultPage.success(postingList);
    }

    @GetMapping("/listHotPosting")
    @ApiOperation(value = "获取热门帖子列表")
    public ResultPage listHotPosting() {
        List<PostingResVO> postingList = postingService.listHosPostingCache();
        return ResultPage.success(postingList);
    }


    @GetMapping("/getPostingDetail/{detailId}")
    @ApiOperation(value = "获取帖子详情")
    public Result<PostingDetailResVO> getPostingDetail(@PathVariable("detailId") String detailId) {
        PostingDetailResVO postingDetail = postingService.getPostingDetail(detailId);
        return Result.success(postingDetail);
    }


    @PostMapping("/addPosting")
    @ApiOperation(value = "新增帖子")
    public Result<Object> addPosting(@RequestBody @Validated PostingAddReqVO postingAddReqVO) {
        postingService.addPosting(postingAddReqVO);
        return Result.success();
    }


    @GetMapping("/likePosting")
    @ApiOperation(value = "点赞")
    public Result<Object> likePosting(@RequestParam String postingId, @RequestParam Integer type) {
        postingService.likePosting(postingId, type);
        return Result.success();
    }


}
